package com.quan.topic;

import com.quan.Config.MQConnection;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

public class Publisher {


    @Test
    public void publisher() throws Exception{
        //创建连接
        Connection conn = MQConnection.getRabbitmaConection();

        //创建channel
        Channel channel = conn.createChannel();

        //创建exchange，绑定队列
        //*占位符 #通配符
        channel.exchangeDeclare("topic-exchange", BuiltinExchangeType.TOPIC);
        channel.queueBind("topic-queue-1","topic-exchange","*.red.*");
        channel.queueBind("topic-queue-2","topic-exchange","fast.#");
        channel.queueBind("topic-queue-2","topic-exchange","*.*.rabbit");

        //发布消息到exchange，同时指定路由规则。
        channel.basicPublish("topic-exchange","fast.red.monkey",null,"红快猴子".getBytes());
        channel.basicPublish("topic-exchange","slow.black.dog",null,"黑慢狗".getBytes());
        channel.basicPublish("topic-exchange","fast.white.rabbit",null,"快白兔".getBytes());


        System.out.println("生产者发布消息成功");

        //释放资源
        channel.close();
        conn.close();

    }
}
